﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FirstPage.aspx.cs" Inherits="Charpter1.FirstPage" %>

<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script runat="server">
        //void Page_Load(object sender ,EventArgs e)
        //{
        //    if (Request.Headers["MyCustomerHeader"] != null)
        //    {
        //        if (Request.Form["passwordtbx"] == "lsyuan"&&Request.Form["usernametbx"] == "lsyuan")

        //        {
        //            Response.Write("lsyuan|hello|333|Yeah|");
        //            Response.End();
        //        }
        //        else
        //        {
        //            throw new Exception("Error Credentials");
        //        }
        //    }
        //}
        void Page_Load(object sender, EventArgs e)
        {
            if (Request.Headers["MyCustomerHeader"] != null)
            {
                if (Request.Form["passwordtbx"] == "lsyuan" && Request.Form["usernametbx"] == "lsyuan")
                {
                    string xml = string.Empty;
                    using (StringWriter sw = new StringWriter())
                    {
                        XmlWriterSettings settings = new XmlWriterSettings();
                        settings.Indent = true;
                        settings.OmitXmlDeclaration = true;
                        using (XmlWriter xw = XmlWriter.Create(sw, settings))
                        {
                            xw.WriteStartDocument();
                            xw.WriteStartElement("employeeinfo");
                            xw.WriteElementString("firstname", "lsyuan");
                            xw.WriteElementString("lastname", "lee");
                            xw.WriteElementString("employeeid", "123");
                            xw.WriteElementString("departname", "kt");
                            xw.WriteEndElement();
                            xw.WriteEndDocument();
                        }
                        xml = sw.ToString();
                    }
                    Response.ContentType = "text/xml";
                    Response.Write(xml);
                    Response.End();
                }
                else
                {
                    throw new Exception("Error Credentials");
                }
            }
        }
    </script>
    <script type="text/javascript">
        var request;
        if (!window.XMLHttpRequest) {
            window.XMLHttpRequest = function window$XMLHttpRequest() {
                var progIDs = [{ 'Msxml2.XMLHTTP': 'Microsoft.XMLHTTP'}];
                for (var i = 0; i < progIDs.length; i++) {
                    try {
                        var xmlHttp = new ActiveXObject(progIDs[i]);
                        return xmlHttp;
                    } catch (e) {

                    }
                    return null;
                }
            }
        }
        window.TestObject = function window$TestObject(username, password) {
            this.username = username;
            this.password = password;
        }
        window.employee = function window$employee(firstname, lastname, employeeid, departname) {
            this.firstname = firstname;
            this.lastname = lastname;
            this.employeeid = employeeid;
            this.departname = departname;

        }
        function deserilize() {
            /*
            //            var delimiter = '|';
            //            var responseIndex = 0;
            //            var delimiterIndex = 0;
            //            var response = request.responseText;
            //            delimiterIndex = response.indexOf(delimiter, responseIndex);
            //            var firstname = response.substring(responseIndex, delimiterIndex);
            //            responseIndex = delimiterIndex + 1;
            //            delimiterIndex = response.indexOf(delimiter, responseIndex);
            //            var lastname = response.substring(responseIndex, delimiterIndex);
            //            responseIndex = delimiterIndex + 1;
            //            delimiterIndex = response.indexOf(delimiter, responseIndex);
            //            var employeeid = response.substring(responseIndex, delimiterIndex);
            //            responseIndex = delimiterIndex + 1;
            //            delimiterIndex = response.indexOf(delimiter, responseIndex);
            //            var departname = response.substring(responseIndex, delimiterIndex);
            //            return new employee(firstname, lastname, employeeid, departname);
            */
            var response = request.responseXML;
            var employeeinfo = response.documentElement;
            var fistnameElement = employeeinfo.childNodes[0];
            var fisrtname = fistnameElement.firstChild.nodeValue;
            var lastnameElement = employeeinfo.childNodes[1];
            var lastname = lastnameElement.firstChild.nodeValue;
            var employeeidElement = employeeinfo.childNodes[2];
            var employeeid = employeeidElement.firstChild.nodeValue;
            var departnameElement = employeeinfo.childNodes[3];
            var departname = departnameElement.firstChild.nodeValue;
            return new employee(fisrtname, lastname, employeeid, departname);
        }
        function readyStateChangeCallBack() {
            if (request.readyState == 4 && request.status == 200) {
                var credentias = document.getElementById("credentialsTable");
                credentias.style.display = "none";
                var employeeinfotable = document.getElementById("infoTable");
                employeeinfotable.style.display = "block";
                var employee = deserilize();
                var fisrtnamespan = document.getElementById("firstname");
                fisrtnamespan.innerText = employee.firstname;
                var lastnamespan = document.getElementById("lastname");
                lastnamespan.innerText = employee.lastname;
                var employeeidspan = document.getElementById("employeeid");
                employeeidspan.innerText = employee.employeeid;
                var departnamespan = document.getElementById("departname");
                departnamespan.innerText = employee.departname;
            }
        }
        window.credentials = function window$credentials(username, password) {
            this.username = username;
            this.password = password;
        }
        function serialize(credentials) {
            var requestBody = "";
            requestBody += "usernametbx";
            requestBody += "=";
            requestBody += encodeURIComponent(credentials.username);
            requestBody += "&";
            requestBody += "passwordtbx";
            requestBody += "=";
            requestBody += encodeURIComponent(credentials.password);
            return requestBody;
        }
        function SubmitMsg() {
            var myObject = new TestObject('lsyuan', 'hello');
            window.alert(myObject.username + ' ' + myObject.password);
        }
        function submitCallBack() {
            var usernametbx = document.getElementById("usernametbx");
            var passwordtbx = document.getElementById("passwordtbx");
            var credentials1 = new credentials(usernametbx.value, passwordtbx.value);
            var body = serialize(credentials1);
            request = new XMLHttpRequest();
            request.open("POST", document.forms[0].action);
            request.onreadystatechange = readyStateChangeCallBack;
            request.setRequestHeader("MyCustomerHeader", "true");
            request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            request.send(body);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center; margin: auto;">
        <table id="credentialsTable" border="0" cellpadding="0" cellspacing="0" style="width: 100%;
            height: 104px;">
            <tr>
                <td style="text-align: right; width: 50%;">
                    UserName:
                </td>
                <td style="text-align: left">
                    <asp:TextBox runat="server" ID="usernametbx" />
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
                    Password:
                </td>
                <td style="text-align: left">
                    <asp:TextBox runat="server" ID="passwordtbx" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="button" name="sumbit" value="submit" onclick="submitCallBack()" />
                </td>
            </tr>
        </table>
        <table id="infoTable" style="display: none" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    fisrtname:
                </td>
                <td>
                    <span id="firstname"></span>
                </td>
            </tr>
            <tr>
                <td>
                    lastname:
                </td>
                <td>
                    <span id="lastname"></span>
                </td>
            </tr>
            <tr>
                <td>
                    employeeid:
                </td>
                <td>
                    <span id="employeeid"></span>
                </td>
            </tr>
            <tr>
                <td>
                    departname:
                </td>
                <td>
                    <span id="departname"></span>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
